package com.seecen.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.pojo.User;
import com.seecen.service.UserService;
import com.seecen.mapper.UserMapper;
import com.seecen.utils.JwtHelper;
import com.seecen.utils.MD5Util;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author 52354
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2025-07-09 16:14:29
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(wrapper);
        //比对用户是否正确
        if(loginUser == null){
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //比对密码是否正确
        //密码是加密的，需要把用户登录传来的明文密码，加密，得到的结果与数据库保存的加密码比较
        //loginUser.getUserPwd():数据库存储的加码的密码
        //MD5Util.encrypt(user.getUserPwd()) 用户 传过来的明文我再进行加密
        if(!StringUtils.isEmpty(user.getUserPwd())
            && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())){
            //登录成功
            //根据用户id生成一个Token
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            HashMap map = new HashMap();
            map.put("token", token);
            return Result.ok(map);
        }
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
    }

    @Override
    public Result getUserInfo(String token) {

        //token是否 有效
        boolean isExpiration = jwtHelper.isExpiration(token);
        if(isExpiration){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }

        //解析出用户Id
        int userId = jwtHelper.getUserId(token).intValue();

        //通过用户Id查用户信息
        User user = userMapper.selectById(userId);
        //把密码设置为空
        user.setUserPwd("");

        HashMap map = new HashMap();
        map.put("loginUser", user);

        return Result.ok(map);
    }

    @Override
    public Result register(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());

        Long count = userMapper.selectCount(wrapper);
        if(count > 0) {
            return  Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        userMapper.insert(user);
        return Result.ok(null);
    }

    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);

        Long count = userMapper.selectCount(wrapper);

        if(count == 0 ){
            return  Result.ok(null);
        }

        return Result.build(null, ResultCodeEnum.USERNAME_USED);
    }
}





